﻿namespace AC.ExtendedRenderer.Toolkit.Drawing
{
    using ComponentFactory.Krypton.Toolkit;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class KryptonColorChooser1 : KryptonForm
    {
        internal KryptonButton btnCancel;
        internal KryptonButton btnOK;
        private ChangeStyle changeType = ChangeStyle.None;
        private Container components;
        private ColorHandler.HSV HSV;
        private bool isInUpdate;
        internal Label Label1;
        internal Label Label2;
        internal Label Label3;
        internal Label Label4;
        internal Label Label5;
        internal Label Label6;
        internal Label Label7;
        private ColorWheel myColorWheel;
        internal NumericUpDown nudBlue;
        internal NumericUpDown nudBrightness;
        internal NumericUpDown nudGreen;
        internal NumericUpDown nudHue;
        internal NumericUpDown nudRed;
        internal NumericUpDown nudSaturation;
        internal Panel pnlBrightness;
        internal Panel pnlColor;
        internal Panel pnlSelectedColor;
        private ColorHandler.RGB RGB;
        private Point selectedPoint;
        private TextBox txtWebColor;

        public KryptonColorChooser1()
        {
            this.InitializeComponent();
        }

        private void ColorChooser1_Load(object sender, EventArgs e)
        {
            base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            base.SetStyle(ControlStyles.UserPaint, true);
            base.SetStyle(ControlStyles.DoubleBuffer, true);
            this.pnlSelectedColor.Visible = false;
            this.pnlBrightness.Visible = false;
            this.pnlColor.Visible = false;
            Rectangle selectedColorRectangle = new Rectangle(this.pnlSelectedColor.Location, this.pnlSelectedColor.Size);
            Rectangle brightnessRectangle = new Rectangle(this.pnlBrightness.Location, this.pnlBrightness.Size);
            Rectangle colorRectangle = new Rectangle(this.pnlColor.Location, this.pnlColor.Size);
            this.myColorWheel = new ColorWheel(colorRectangle, brightnessRectangle, selectedColorRectangle);
            this.myColorWheel.ColorChanged = (ColorWheel.ColorChangedEventHandler) Delegate.Combine(this.myColorWheel.ColorChanged, new ColorWheel.ColorChangedEventHandler(this.myColorWheel_ColorChanged));
            this.SetRGB(this.RGB);
            this.SetHSV(this.HSV);
        }

        private void ColorChooser1_Paint(object sender, PaintEventArgs e)
        {
            switch (this.changeType)
            {
                case ChangeStyle.MouseMove:
                case ChangeStyle.None:
                    this.myColorWheel.Draw(e.Graphics, this.selectedPoint);
                    return;

                case ChangeStyle.RGB:
                    this.myColorWheel.Draw(e.Graphics, this.RGB);
                    return;

                case ChangeStyle.HSV:
                    this.myColorWheel.Draw(e.Graphics, this.HSV);
                    return;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void frmMain_MouseUp(object sender, MouseEventArgs e)
        {
            this.myColorWheel.SetMouseUp();
            this.changeType = ChangeStyle.None;
        }

        private void HandleHSVChange(object sender, EventArgs e)
        {
            if (!this.isInUpdate)
            {
                this.changeType = ChangeStyle.HSV;
                this.HSV = new ColorHandler.HSV((int) this.nudHue.Value, (int) this.nudSaturation.Value, (int) this.nudBrightness.Value);
                this.SetRGB(ColorHandler.HSVtoRGB(this.HSV));
                base.Invalidate();
            }
        }

        private void HandleMouse(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.changeType = ChangeStyle.MouseMove;
                this.selectedPoint = new Point(e.X, e.Y);
                base.Invalidate();
            }
        }

        private void HandleRGBChange(object sender, EventArgs e)
        {
            if (!this.isInUpdate)
            {
                this.changeType = ChangeStyle.RGB;
                this.RGB = new ColorHandler.RGB((int) this.nudRed.Value, (int) this.nudGreen.Value, (int) this.nudBlue.Value);
                this.SetHSV(ColorHandler.RGBtoHSV(this.RGB));
                base.Invalidate();
            }
        }

        private void HandleTextChanged(object sender, EventArgs e)
        {
            decimal decimal1 = ((NumericUpDown) sender).Value;
        }

        private void InitializeComponent()
        {
            this.btnCancel = new KryptonButton();
            this.btnOK = new KryptonButton();
            this.Label3 = new Label();
            this.nudSaturation = new NumericUpDown();
            this.Label7 = new Label();
            this.nudBrightness = new NumericUpDown();
            this.nudRed = new NumericUpDown();
            this.pnlColor = new Panel();
            this.Label6 = new Label();
            this.Label1 = new Label();
            this.Label5 = new Label();
            this.pnlSelectedColor = new Panel();
            this.pnlBrightness = new Panel();
            this.nudBlue = new NumericUpDown();
            this.Label4 = new Label();
            this.nudGreen = new NumericUpDown();
            this.Label2 = new Label();
            this.nudHue = new NumericUpDown();
            this.txtWebColor = new TextBox();
            this.nudSaturation.BeginInit();
            this.nudBrightness.BeginInit();
            this.nudRed.BeginInit();
            this.nudBlue.BeginInit();
            this.nudGreen.BeginInit();
            this.nudHue.BeginInit();
            base.SuspendLayout();
            this.btnCancel.DialogResult = DialogResult.Cancel;
            this.btnCancel.Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.btnCancel.Location = new Point(0xc0, 320);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x40, 0x18);
            this.btnCancel.TabIndex = 0x37;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Values.ExtraText = "";
            this.btnCancel.Values.Image = null;
            this.btnCancel.Values.ImageStates.ImageCheckedNormal = null;
            this.btnCancel.Values.ImageStates.ImageCheckedPressed = null;
            this.btnCancel.Values.ImageStates.ImageCheckedTracking = null;
            this.btnCancel.Values.Text = "Cancel";
            this.btnOK.DialogResult = DialogResult.OK;
            this.btnOK.Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.btnOK.Location = new Point(120, 320);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new Size(0x40, 0x18);
            this.btnOK.TabIndex = 0x36;
            this.btnOK.Text = "OK";
            this.btnOK.Values.ExtraText = "";
            this.btnOK.Values.Image = null;
            this.btnOK.Values.ImageStates.ImageCheckedNormal = null;
            this.btnOK.Values.ImageStates.ImageCheckedPressed = null;
            this.btnOK.Values.ImageStates.ImageCheckedTracking = null;
            this.btnOK.Values.Text = "OK";
            this.Label3.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.Label3.Location = new Point(0x98, 280);
            this.Label3.Name = "Label3";
            this.Label3.Size = new Size(0x30, 0x17);
            this.Label3.TabIndex = 0x2d;
            this.Label3.Text = "Blue:";
            this.Label3.TextAlign = ContentAlignment.MiddleLeft;
            this.nudSaturation.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.nudSaturation.Location = new Point(0x60, 0x100);
            int[] bits = new int[4];
            bits[0] = 0xff;
            this.nudSaturation.Maximum = new decimal(bits);
            this.nudSaturation.Name = "nudSaturation";
            this.nudSaturation.Size = new Size(0x30, 0x16);
            this.nudSaturation.TabIndex = 0x2a;
            this.nudSaturation.ValueChanged += new EventHandler(this.HandleHSVChange);
            this.nudSaturation.TextChanged += new EventHandler(this.HandleTextChanged);
            this.Label7.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.Label7.Location = new Point(0x10, 280);
            this.Label7.Name = "Label7";
            this.Label7.Size = new Size(0x48, 0x17);
            this.Label7.TabIndex = 50;
            this.Label7.Text = "Brightness:";
            this.Label7.TextAlign = ContentAlignment.MiddleLeft;
            this.nudBrightness.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.nudBrightness.Location = new Point(0x60, 280);
            int[] numArray2 = new int[4];
            numArray2[0] = 0xff;
            this.nudBrightness.Maximum = new decimal(numArray2);
            this.nudBrightness.Name = "nudBrightness";
            this.nudBrightness.Size = new Size(0x30, 0x16);
            this.nudBrightness.TabIndex = 0x2f;
            this.nudBrightness.ValueChanged += new EventHandler(this.HandleHSVChange);
            this.nudBrightness.TextChanged += new EventHandler(this.HandleTextChanged);
            this.nudRed.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.nudRed.Location = new Point(0xd0, 0xe8);
            int[] numArray3 = new int[4];
            numArray3[0] = 0xff;
            this.nudRed.Maximum = new decimal(numArray3);
            this.nudRed.Name = "nudRed";
            this.nudRed.Size = new Size(0x30, 0x16);
            this.nudRed.TabIndex = 0x26;
            this.nudRed.ValueChanged += new EventHandler(this.HandleRGBChange);
            this.nudRed.TextChanged += new EventHandler(this.HandleTextChanged);
            this.pnlColor.Location = new Point(8, 8);
            this.pnlColor.Name = "pnlColor";
            this.pnlColor.Size = new Size(0xb0, 0xb0);
            this.pnlColor.TabIndex = 0x33;
            this.pnlColor.Visible = false;
            this.pnlColor.MouseUp += new MouseEventHandler(this.frmMain_MouseUp);
            this.Label6.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.Label6.Location = new Point(0x10, 0x100);
            this.Label6.Name = "Label6";
            this.Label6.Size = new Size(0x48, 0x17);
            this.Label6.TabIndex = 0x31;
            this.Label6.Text = "Saturation:";
            this.Label6.TextAlign = ContentAlignment.MiddleLeft;
            this.Label1.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.Label1.Location = new Point(0x98, 0xe8);
            this.Label1.Name = "Label1";
            this.Label1.Size = new Size(0x30, 0x17);
            this.Label1.TabIndex = 0x2b;
            this.Label1.Text = "Red:";
            this.Label1.TextAlign = ContentAlignment.MiddleLeft;
            this.Label5.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.Label5.Location = new Point(0x10, 0xe8);
            this.Label5.Name = "Label5";
            this.Label5.Size = new Size(0x48, 0x17);
            this.Label5.TabIndex = 0x30;
            this.Label5.Text = "Hue:";
            this.Label5.TextAlign = ContentAlignment.MiddleLeft;
            this.pnlSelectedColor.Location = new Point(0x88, 0xcb);
            this.pnlSelectedColor.Name = "pnlSelectedColor";
            this.pnlSelectedColor.Size = new Size(0x30, 20);
            this.pnlSelectedColor.TabIndex = 0x35;
            this.pnlSelectedColor.Visible = false;
            this.pnlBrightness.Location = new Point(0xd0, 8);
            this.pnlBrightness.Name = "pnlBrightness";
            this.pnlBrightness.Size = new Size(0x10, 0xb0);
            this.pnlBrightness.TabIndex = 0x34;
            this.pnlBrightness.Visible = false;
            this.nudBlue.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.nudBlue.Location = new Point(0xd0, 280);
            int[] numArray4 = new int[4];
            numArray4[0] = 0xff;
            this.nudBlue.Maximum = new decimal(numArray4);
            this.nudBlue.Name = "nudBlue";
            this.nudBlue.Size = new Size(0x30, 0x16);
            this.nudBlue.TabIndex = 40;
            this.nudBlue.ValueChanged += new EventHandler(this.HandleRGBChange);
            this.nudBlue.TextChanged += new EventHandler(this.HandleTextChanged);
            this.Label4.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.Label4.Location = new Point(0x52, 0xcb);
            this.Label4.Name = "Label4";
            this.Label4.Size = new Size(0x30, 20);
            this.Label4.TabIndex = 0x2e;
            this.Label4.Text = "Color:";
            this.Label4.TextAlign = ContentAlignment.MiddleLeft;
            this.nudGreen.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.nudGreen.Location = new Point(0xd0, 0x100);
            int[] numArray5 = new int[4];
            numArray5[0] = 0xff;
            this.nudGreen.Maximum = new decimal(numArray5);
            this.nudGreen.Name = "nudGreen";
            this.nudGreen.Size = new Size(0x30, 0x16);
            this.nudGreen.TabIndex = 0x27;
            this.nudGreen.ValueChanged += new EventHandler(this.HandleRGBChange);
            this.nudGreen.TextChanged += new EventHandler(this.HandleTextChanged);
            this.Label2.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.Label2.Location = new Point(0x98, 0x100);
            this.Label2.Name = "Label2";
            this.Label2.Size = new Size(0x30, 0x17);
            this.Label2.TabIndex = 0x2c;
            this.Label2.Text = "Green:";
            this.Label2.TextAlign = ContentAlignment.MiddleLeft;
            this.nudHue.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.nudHue.Location = new Point(0x60, 0xe8);
            int[] numArray6 = new int[4];
            numArray6[0] = 0xff;
            this.nudHue.Maximum = new decimal(numArray6);
            this.nudHue.Name = "nudHue";
            this.nudHue.Size = new Size(0x30, 0x16);
            this.nudHue.TabIndex = 0x29;
            this.nudHue.ValueChanged += new EventHandler(this.HandleHSVChange);
            this.nudHue.TextChanged += new EventHandler(this.HandleTextChanged);
            this.txtWebColor.Location = new Point(190, 0xcb);
            this.txtWebColor.Name = "txtWebColor";
            this.txtWebColor.Size = new Size(0x42, 20);
            this.txtWebColor.TabIndex = 0x38;
            this.AutoScaleBaseSize = new Size(5, 13);
            this.BackColor = System.Drawing.Color.White;
            base.ClientSize = new Size(0x108, 0x15d);
            base.Controls.Add(this.txtWebColor);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnOK);
            base.Controls.Add(this.Label3);
            base.Controls.Add(this.nudSaturation);
            base.Controls.Add(this.Label7);
            base.Controls.Add(this.nudBrightness);
            base.Controls.Add(this.nudRed);
            base.Controls.Add(this.pnlColor);
            base.Controls.Add(this.Label6);
            base.Controls.Add(this.Label1);
            base.Controls.Add(this.Label5);
            base.Controls.Add(this.pnlSelectedColor);
            base.Controls.Add(this.pnlBrightness);
            base.Controls.Add(this.nudBlue);
            base.Controls.Add(this.Label4);
            base.Controls.Add(this.nudGreen);
            base.Controls.Add(this.Label2);
            base.Controls.Add(this.nudHue);
            base.FormBorderStyle = FormBorderStyle.FixedDialog;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "KryptonColorChooser1";
            this.Text = "Select Color";
            base.Paint += new PaintEventHandler(this.ColorChooser1_Paint);
            base.MouseUp += new MouseEventHandler(this.frmMain_MouseUp);
            base.MouseMove += new MouseEventHandler(this.HandleMouse);
            base.MouseDown += new MouseEventHandler(this.HandleMouse);
            base.Load += new EventHandler(this.ColorChooser1_Load);
            this.nudSaturation.EndInit();
            this.nudBrightness.EndInit();
            this.nudRed.EndInit();
            this.nudBlue.EndInit();
            this.nudGreen.EndInit();
            this.nudHue.EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void myColorWheel_ColorChanged(object sender, ColorChangedEventArgs e)
        {
            this.SetRGB(e.RGB);
            this.SetHSV(e.HSV);
        }

        private void RefreshValue(NumericUpDown nud, int value)
        {
            if (nud.Value != value)
            {
                nud.Value = value;
                nud.Refresh();
            }
        }

        private void SetHSV(ColorHandler.HSV HSV)
        {
            this.isInUpdate = true;
            this.RefreshValue(this.nudHue, HSV.Hue);
            this.RefreshValue(this.nudSaturation, HSV.Saturation);
            this.RefreshValue(this.nudBrightness, HSV.value);
            this.isInUpdate = false;
        }

        private void SetRGB(ColorHandler.RGB RGB)
        {
            this.isInUpdate = true;
            this.RefreshValue(this.nudRed, RGB.Red);
            this.RefreshValue(this.nudBlue, RGB.Blue);
            this.RefreshValue(this.nudGreen, RGB.Green);
            this.isInUpdate = false;
            this.txtWebColor.Text = ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(RGB.Red, RGB.Green, RGB.Blue)).ToString();
        }

        public System.Drawing.Color Color
        {
            get
            {
                return this.myColorWheel.Color;
            }
            set
            {
                this.changeType = ChangeStyle.RGB;
                this.RGB = new ColorHandler.RGB(value.R, value.G, value.B);
                this.HSV = ColorHandler.RGBtoHSV(this.RGB);
            }
        }

        private enum ChangeStyle
        {
            MouseMove,
            RGB,
            HSV,
            None
        }
    }
}

